package com.fxp.project.jiuselu.web.business.v1.controller.order;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxp.project.express.entity.request.QueryTrackParam;
import com.fxp.project.express.entity.response.QueryTrackData;
import com.fxp.project.express.entity.response.QueryTrackResp;
import com.fxp.project.express.service.impl.QueryTrackService;
import com.fxp.project.jiuselu.web.base.controller.BaseController;
import com.fxp.project.jiuselu.web.business.v1.request.order.ReqOrderDetail;
import com.fxp.project.jiuselu.web.domain.CommonResult;
import com.fxp.project.jiuselu.web.models.order.entity.JslOrderArrears;
import com.fxp.project.jiuselu.web.models.order.entity.SplitOrder;
import com.fxp.project.jiuselu.web.models.order.service.IJslOrderService;
import com.fxp.project.jiuselu.web.models.order.service.ISplitOrderService;
import com.fxp.project.jiuselu.web.models.order.vo.JslOrderVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

/**
 * 拆分发货Controller
 *
 * @author Jason.Wang
 * @date 2022-06-15
 */
@Log4j2
@RestController
@RequestMapping("/system/split/order")
public class SplitOrderController extends BaseController
{
    @Autowired
    private ISplitOrderService splitOrderService;

    @Autowired
    QueryTrackService queryTrackService;

    @Autowired
    IJslOrderService jslOrderService;

    /**
     * 查询拆分发货列表
     */
    @PostMapping("/list")
    @CrossOrigin
    public CommonResult list(SplitOrder splitOrder)
    {
        List<SplitOrder> list = splitOrderService.selectSplitOrderList(splitOrder);
        return CommonResult.success(list);
    }


    /**
     * 查询拆分发货列表
     */
    @PostMapping("/selectOne")
    @CrossOrigin
    public CommonResult selectOne(@RequestBody @Validated SplitOrder splitOrder)
    {
        SplitOrder one = splitOrderService.getOne(new QueryWrapper<SplitOrder>().eq("order_id", splitOrder.getOrderId()).eq("code", 1).last("limit 1"));
        return CommonResult.success(one);
    }

    /**
     * orderDetail: 查询当前订单详情<br/>
     *
     * @param reqOrderDetail
     * @param result
     * @return
     * @author Jason.Wang
     * @date 2021/12/26 12:06
     */
    @PostMapping("detail")
    @CrossOrigin
    public CommonResult orderDetail(@RequestBody @Validated ReqOrderDetail reqOrderDetail, BindingResult result) {
        Long userId;
        Long id = reqOrderDetail.getSpiltId();
        SplitOrder splitOrder = splitOrderService.selectSplitOrderById(id);

        userId = jslOrderService.getById(splitOrder.getOrderId()).getUserId();

        super.checkBindResult(result);
        JslOrderVo jslOrderVo = jslOrderService.selectJslOrderVoByUserIdAndOrderId(splitOrder.getOrderId(), userId);

//        SplitOrder splitOrder = splitOrderService.selectSplitOrderByOrderId(jslOrderVo.getOrderId());
        List<QueryTrackData> express = new ArrayList<>();
        // 判断当前是否存在物流信息
        if (jslOrderVo.getLogisticStatus() == 1) {
            QueryTrackParam queryTrackParam = QueryTrackParam.builder().com(splitOrder.getLogisticCode()).num(splitOrder.getLogisticNum()).phone(jslOrderVo.getPhone()).build();
            QueryTrackResp queryTrackResp = null;
            try {
                queryTrackResp = queryTrackService.queryTrack(queryTrackParam);
                if ("ok".equalsIgnoreCase(queryTrackResp.getMessage()) && "200".equals(queryTrackResp.getStatus())) {
                    express = queryTrackResp.getData();
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error(e);
            }
        }
        JSONObject data = new JSONObject();
        data.put("order", jslOrderVo);
        data.put("express", express);
        if (jslOrderVo.getPayStatus() == 0) {
            long surplusMills = jslOrderVo.getAddTime().toEpochSecond(ZoneOffset.of("+8")) + 86400 - LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
            System.out.println("surplusMills--------->" + surplusMills);
            data.put("surplusMills", surplusMills);
        }
        return CommonResult.success(data);
    }


}
